package com.mdd.admin.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.mdd.admin.service.order.IOrderClueService;
import com.mdd.admin.vo.execl.OrderClueImport;
import com.mdd.common.entity.order.OrderClue;
import com.mdd.common.util.StringUtils;
import com.mdd.system.service.dict.ISettingDictDataService;
import com.mdd.system.vo.dict.SettingDictDataVo;
import java.util.List;
import java.util.Objects;

public class OrderClueListener extends AnalysisEventListener<OrderClueImport> {
    //最大缓存量
    private static final int BATCH_COUNT = 100;
    //缓存数据
    private List<OrderClue> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    private IOrderClueService iOrderClueService;
    private ISettingDictDataService iSettingDictDataService;
    public OrderClueListener(IOrderClueService orderClueService, ISettingDictDataService settingDictDataService){
            this.iOrderClueService = orderClueService;
            this.iSettingDictDataService = settingDictDataService;
    }

    @Override
    public void invoke(OrderClueImport data, AnalysisContext context) {
            OrderClue orderClue = new OrderClue();
            orderClue.setDriverName(data.getDriverName());
            orderClue.setDeptId(1L);
            orderClue.setDeptCode("1");
            orderClue.setQualificationCertificate(transQualificationCertificate(data.getQualificationCertificate()));
            orderClue.setWillingCity(data.getWillingCity());
            orderClue.setPhoneNumber(data.getPhoneNumber());
            orderClue.setAvailableDate(Integer.valueOf(data.getAvailableDate()));
            orderClue.setCarFrom(transCarFrom(data.getCarFrom()));
            orderClue.setIntentionPlatform(data.getIntentionPlatform());
            orderClue.setLicensePeriod(Integer.valueOf(data.getLicensePeriod()));
            orderClue.setRemark(data.getRemark());
            orderClue.setChannel(Integer.valueOf(transChannel(data.getChannel())));

            // 每解析一行数据,该方法会被调用一次
            cachedDataList.add(orderClue);
            // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
            if (cachedDataList.size() >= BATCH_COUNT) {
                saveData();
                // 存储完成清理 list
                cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
            }
    }

    private void saveData(){
        iOrderClueService.batchInsert(cachedDataList);
        cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
    }

    // 0 无 1 有
    private Integer transQualificationCertificate(String qualificationCertificate){
            if(StringUtils.isEmpty(qualificationCertificate)){
                return null;
            }else if(StringUtils.equals(qualificationCertificate,"有")){
                return 1;
            }else if(StringUtils.equals(qualificationCertificate,"无")){
                return 0;
            }
        return null;
    }
    //0 自持/1 租赁/2其它
    private Integer transCarFrom(String carFrom){
        if(StringUtils.isEmpty(carFrom)){
            return null;
        }else if(StringUtils.equals(carFrom,"自持")){
            return 0;
        }else if(StringUtils.equals(carFrom,"租赁")){
            return 1;
        }else  if(StringUtils.equals(carFrom,"其它")){
            return 2;
        }
        return null;
    }
    //0 自持/1 租赁/2其它
    private String transChannel(String channel){
        if(StringUtils.isEmpty(channel)){
            return null;
        }else {
            SettingDictDataVo settingDictDataVo =  iSettingDictDataService.findByTypeAndValue("clue_channel",channel);
            if(Objects.nonNull(settingDictDataVo)){
                return  settingDictDataVo.getValue();
            }
        }
        return null;
    }
}
